package com.zkingsoft.zkingbase.activities;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.zkingsoft.zkingbase.R;
import com.zkingsoft.zkingbase.base.BaseActivity;
import com.zkingsoft.zkingbase.bean.User;
import com.zkingsoft.zkingbase.constant.Constant;
import com.zkingsoft.zkingbase.manager.GreenDaoManager;
import com.zkingsoft.zkingbase.manager.OkHttpManager;
import com.zkingsoft.zkingbase.model.UserRegisterModel;
import com.zkingsoft.zkingbase.presenter.UserRegisterPresenter;
import com.zkingsoft.zkingbase.utils.MD5Util;
import com.zkingsoft.zkingbase.utils.PreferenceUtils;
import com.zkingsoft.zkingbase.utils.RegaxUtils;
import com.zkingsoft.zkingbase.view.IPhoneRegistView;
import com.zkingsoft.zkingbase.widget.NormalInputBox;

import butterknife.BindView;
import butterknife.OnClick;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

public class IPhoneRegistActivity extends BaseActivity implements IPhoneRegistView<User>
{

    @BindView(R.id.nb_number)
    NormalInputBox phone;
    @BindView(R.id.nb_pwd)
    NormalInputBox pwd;
    @BindView(R.id.varicode)
    NormalInputBox varicode;

    private OkHttpManager manager;
    //短信验证码事件监听
    private EventHandler eh;

    private UserRegisterPresenter<IPhoneRegistView, User> presenter;
    private UserRegisterModel registModel;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        manager = OkHttpManager.getInstance();

        //短信验证码初始化
        SMSSDK.initSDK(this, Constant.MOB_KEY, Constant.MOB_SECRET);

        varicode.setOnVarificationClickListener(new NormalInputBox.OnVarificationClickListener()
        {
            @Override
            public boolean onSendVarificationCode()
            {
                String num = phone.getContent();
                if (RegaxUtils.isPhoneNumber(num))
                {
                    SMSSDK.getVerificationCode("86", phone.getContent());
                    return true;
                } else
                {
                    Toast.makeText(IPhoneRegistActivity.this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
                    return false;
                }
            }
        });

        eh = new EventHandler()
        {
            @Override
            public void afterEvent(int event, int result, Object data)
            {

                if (result == SMSSDK.RESULT_COMPLETE)
                {
                    //回调完成
                    if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE)
                    {
                        mHandler.sendEmptyMessage(0);
                    } else if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE)
                    {
                        //验证通过
                        mHandler.sendEmptyMessage(1);
                    }
                }
            }
        };
        SMSSDK.registerEventHandler(eh); //注册短信回调

        registModel = new UserRegisterModel();
        presenter = new UserRegisterPresenter<>(this, this);
    }

    @Override
    public int getContentView()
    {
        return R.layout.activity_phone_regist;
    }

    private Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case 0:
                    Toast.makeText(IPhoneRegistActivity.this, "获取验证码", Toast.LENGTH_SHORT).show();
                    break;
                case 1:

                    break;
                case 2:

                    break;
            }
        }
    };

    @OnClick(value = R.id.btn_next)
    public void click(View v)
    {
        switch (v.getId())
        {
            case R.id.btn_next:
                final String code = varicode.getContent();
                if(TextUtils.isEmpty(phone.getContent()))
                {
                    Toast.makeText(IPhoneRegistActivity.this, "请输入手机号码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(code))
                {
                    Toast.makeText(IPhoneRegistActivity.this, "请输入验证码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(pwd.getContent()))
                {
                    Toast.makeText(IPhoneRegistActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }

                registModel.registerByPhone(getPhoneNumber(), getPassword(), getVariCode(), presenter);
                break;
        }
    }

    @Override
    protected void onDestroy()
    {
        SMSSDK.unregisterAllEventHandler();
        super.onDestroy();
    }

    @Override
    public String getPhoneNumber()
    {
        return phone.getContent();
    }

    @Override
    public String getPassword()
    {
        return MD5Util.soltMd5(pwd.getContent());
    }

    @Override
    public String getVariCode()
    {
        return varicode.getContent();
    }

    @Override
    public void loadSuccess(User data, Object obj)
    {
        GreenDaoManager manager = GreenDaoManager.getInstance(this, Constant.DB_DEFAULT);
        manager.getDaoSession().getUserDao().insert(data);

        //标记已经登录
        PreferenceUtils.put(Constant.FLAG_IS_LOGIN, true);
        //记录用户token
        PreferenceUtils.put(Constant.FLAG_TOKEN, data.token);
        //记录用户id，方便以后删除时直接根据id删除
        PreferenceUtils.put(Constant.FlAG_USER_ID, data.suId);

        Intent intent = new Intent(this, InfoActivity.class);
        intent.putExtra("userinfo", data);
        startActivity(intent);
    }

    @Override
    public void loadFailed(String msg, Throwable e)
    {
        toast(msg, e);
    }
}
